package com.test.particles;

import java.util.LinkedList;

import javax.vecmath.Point2f;
import javax.vecmath.Point2i;

public class UniformGrid {
	public LinkedList<Particle>[][] mCellParticles;
	
	UniformGrid(Point2f origin, Point2f size)
	{
		mOrigin = origin;
		mSize = size;
		mRes = new Point2i();
		
		double radius = 0.04f;
		
		mRes.x = (int) Math.ceil((double) mSize.x / radius);
		mRes.y = (int) Math.ceil((double) mSize.y / radius);
		
		mSize = new Point2f((float) radius * mRes.x, (float) radius * mRes.y);
		
		mCellParticles = (LinkedList<Particle>[][]) new LinkedList[mRes.x][mRes.y];
	}
	
	public void addParticle(Particle particle)
	{
		Point2i cell = new Point2i();
		
		cell.x = (int) ((particle.getPos().x - mOrigin.x) / mSize.x);
		cell.y = (int) ((particle.getPos().y - mOrigin.y) / mSize.y);
		
		if(cell.x >= 0 && cell.x < mRes.x &&
				cell.y >= 0 && cell.y < mRes.y)
		{
			mCellParticles[cell.x][cell.y].addLast(particle);
			
			particle.setCell(cell);
		}
	}
	
	public LinkedList<Particle> getCellParticles(Point2i cell)
	{
		return mCellParticles[cell.x][cell.y];
	}
	
	public void clear()
	{
		for(int x = 0; x < mRes.x; x++)
		{
			for(int y = 0; y < mRes.y; y++)
			{
				mCellParticles[x][y].clear();
			}
		}
	}
	
	Point2f mOrigin;
	Point2f mSize;
	Point2i mRes;
	
}